#include "page5_widget/page5_widget.h"
#include "user/welcomewidget.h"


Page5_Widget::Page5_Widget(WelcomeWidget *widget)
{
    this->welcomewidget = widget;
    welcome_ui = this->welcomewidget->ui;

    connect(welcome_ui->pb_update,SIGNAL(clicked()),this,SLOT(on_pb_update_clicked()));     //建立信号槽
    connect(welcome_ui->pb_time_update,SIGNAL(clicked()),this,SLOT(on_pb_time_update_clicked()));     //建立信号槽
    connect(welcome_ui->pb_set_master,SIGNAL(clicked()),this,SLOT(on_pb_set_master_clicked()));     //建立信号槽
    connect(welcome_ui->pb_master_scan,SIGNAL(clicked()),this,SLOT(on_pb_master_scan_clicked()));     //建立信号槽
    connect(welcome_ui->pb_reset_master,SIGNAL(clicked()),this,SLOT(on_pb_reset_master_clicked()));     //建立信号槽

    connect(this,SIGNAL(signal_write_sp_tx_queue(char*,unsigned int)),welcomewidget->m_serial_tx_thread,SLOT(slot_write_serial_port_tx_queue(char*, unsigned int)));

    welcome_ui->pb_master_scan->setCheckable(false);


    QString info;

    info = trUtf8("  软件版本 ") + SOFTWARE_VERSION + trUtf8("  硬件版本 ") + HARDWARE_VERSION;

    welcome_ui->label_info->setText(info);
}

Page5_Widget::~Page5_Widget()
{


}


//定时器超时处理
void Page5_Widget::buttontimer_timeout_handler()
{
    welcome_ui->pb_master_scan->setText("主机扫描");

    welcome_ui->pb_master_scan->setEnabled(true);
    welcome_ui->pb_set_master->setEnabled(true);
}


void Page5_Widget::on_pb_update_clicked()
{
    //qDebug() << "on_pb_update_clicked()";

    Update_Dialog *update_dialog = new Update_Dialog();

    update_dialog->exec();

    delete update_dialog;
}


void Page5_Widget::on_pb_time_update_clicked()
{
    //welcome_ui->label_time->setText(trUtf8("正在进行时间同步..."));

    QProcess proc;

    proc.execute("ntpdate 202.112.29.82");

    if(proc.waitForFinished(30000))
    {
        qDebug() << "time update timeout";
        proc.close();

        //QMessageBox::information(new Update_Dialog(), "INFORMATION", tr("<font color='white'>连接服务器超时</font>"));

        QMessageBox msg_box(QMessageBox::Information,
                           "",
                           tr("<font color='white'>连接服务器超时</font>"),
                           QMessageBox::Ok, new Update_Dialog(),
                           Qt::FramelessWindowHint);
        msg_box.exec();
    }
    else
    {
        if(proc.exitStatus() == QProcess::CrashExit)
        {
            qDebug() << "time update fail";
            proc.close();

            //QMessageBox::information(new Update_Dialog(), "INFORMATION", tr("<font color='white'>时间同步失败</font>"));

            QMessageBox msg_box(QMessageBox::Information,
                               "",
                               tr("<font color='white'>时间同步失败</font>"),
                               QMessageBox::Ok, new Update_Dialog(),
                               Qt::FramelessWindowHint);
            msg_box.exec();
        }
        else
        {
            qDebug() << "time update success";
            proc.start("hwclock --systohc");

            //QMessageBox::information(new Update_Dialog(), "INFORMATION", tr("<font color='white'>时间同步成功</font>"));
            QMessageBox msg_box(QMessageBox::Information,
                               "",
                               tr("<font color='white'>时间同步成功</font>"),
                               QMessageBox::Ok, new Update_Dialog(),
                               Qt::FramelessWindowHint);
            msg_box.exec();

            if(proc.waitForFinished())
            {
                proc.close();
            }
            else
            {
                if(proc.exitStatus() == QProcess::CrashExit)
                {
                    proc.close();
                }
                else
                {
                }
            }
        }
    }

    proc.close();

//    connect(&proc,SIGNAL(finished(int, QProcess::ExitStatus)),this,SLOT(slot_time_update_finished(int, QProcess::ExitStatus)));     //建立信号槽
}


void Page5_Widget::slot_time_update_finished(int exit_code, QProcess::ExitStatus exit_status)
{
    qDebug() << exit_code << exit_status;

    //QMessageBox::information(this, "INFORMATION", tr("<font color='white'>时间同步成功</font>"));

    QProcess proc;

    proc.startDetached("hwclock --systohc");

}



void Page5_Widget::on_pb_set_master_clicked()
{
    SetMaster_Dialog *set_master_dialog = new SetMaster_Dialog(welcomewidget->m_serial_tx_thread,welcomewidget->m_serial_rx_thread);

    set_master_dialog->exec();

    delete set_master_dialog;
}

void Page5_Widget::on_pb_reset_master_clicked()
{
    static char data_buff[20];

    memset(data_buff,0x00,sizeof(data_buff));

    data_buff[0] = 'A';
    data_buff[1] = 'T';
    data_buff[2] = '+';
    data_buff[3] = 'R';
    data_buff[4] = 'E';
    data_buff[5] = 'S';
    data_buff[6] = 'E';
    data_buff[7] = 'T';
    data_buff[8] = '\r';
    data_buff[9] = '\n';

    //strncpy(data_buff,"AT+RESET\r\n",sizeof("AT+RESET\r\n"));

    emit signal_write_sp_tx_queue(data_buff,10);
}

void Page5_Widget::on_pb_master_scan_clicked()
{
    static char data_buff[20];

    if(welcome_ui->pb_master_scan->isCheckable())
    {
        welcome_ui->pb_master_scan->setCheckable(false);

        memset(data_buff,0x00,sizeof(data_buff));

        data_buff[0] = 'A';
        data_buff[1] = 'T';
        data_buff[2] = '+';
        data_buff[3] = 'S';
        data_buff[4] = 'C';
        data_buff[5] = 'A';
        data_buff[6] = 'N';
        data_buff[7] = '_';
        data_buff[8] = 'S';
        data_buff[9] = 'T';
        data_buff[10] = 'O';
        data_buff[11] = 'P';
        data_buff[12] = '\r';
        data_buff[13] = '\n';

        //strncpy(data_buff,"AT+SCAN_STOP\r\n",sizeof("AT+SCAN_STOP\r\n"));

        emit signal_write_sp_tx_queue(data_buff,14);

        welcome_ui->pb_master_scan->setText("开始扫描");
        welcome_ui->pb_set_master->setEnabled(true);
    }
    else
    {
        welcome_ui->pb_master_scan->setCheckable(true);

        memset(data_buff,0x00,sizeof(data_buff));

        data_buff[0] = 'A';
        data_buff[1] = 'T';
        data_buff[2] = '+';
        data_buff[3] = 'S';
        data_buff[4] = 'C';
        data_buff[5] = 'A';
        data_buff[6] = 'N';
        data_buff[7] = '_';
        data_buff[8] = 'S';
        data_buff[9] = 'T';
        data_buff[10] = 'A';
        data_buff[11] = 'R';
        data_buff[12] = 'T';
        data_buff[13] = '\r';
        data_buff[14] = '\n';

        //strncpy(data_buff,"AT+SCAN_START\r\n",sizeof("AT+SCAN_START\r\n"));

        emit signal_write_sp_tx_queue(data_buff,15);

        welcome_ui->pb_master_scan->setText("停止扫描");
        welcome_ui->pb_set_master->setEnabled(false);
    }
}





